package com.deprice4u_client.products_on_sale;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.deprice4u_client.MainTabsFragment;
import com.deprice4u_client.R;

public class SocialListViewActivity extends Fragment {

	static String m_tag = "[TAG]";
	private SocialSearcher mSearcher;
	private ListView mLVSearchResult;

	private View v;  
	public String queryKeyword;
	public Pair<ArrayList<SocialDataClass>, ArrayList<String>> searchResultPair;
	public SocialSearchAdapter adapter;

	public SocialListViewActivity() {
		queryKeyword = null;
		searchResultPair=null;
	}

	public SocialListViewActivity(String queryKeyword) {
		this.queryKeyword = queryKeyword;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
			// Currently in a layout without a container, so no
			// reason to create our view.
			return null;
		}
		v = inflater.inflate(R.layout.on_sale_main, null);
		
		MainTabsFragment fragment = (MainTabsFragment)getFragmentManager().findFragmentById(R.id.tabs_fragment);
		if(fragment.isFromSearch){
			this.queryKeyword = fragment.queryFromSearch;
			fragment.isFromSearch = false;
		}

		mSearcher = new SocialSearcher();
		mLVSearchResult = (ListView) v.findViewById(R.id.lSearchResult);
		ArrayList<SocialDataClass> mListItems = null;
		ArrayList<String> arryRelatedKeyword = null;
		if (this.queryKeyword == null) {
			searchResultPair = mSearcher.searchByKeyword(null, 0, 30);
			if(searchResultPair == null){
				Log.i("SM","No search results from Server");
				return v;
			}
			mListItems = searchResultPair.first;
		} else {
			searchResultPair = mSearcher.searchByKeyword(this.queryKeyword, 0, 30);
			if(searchResultPair == null){
				Log.i("SM","No search results from Server");
				return v;
			}
			mListItems = searchResultPair.first;
			arryRelatedKeyword = searchResultPair.second;
		}

		if (mListItems == null) {
			Log.i("SM", "No results from server");
			return v;
		}
		adapter = new SocialSearchAdapter(getActivity(),
				mListItems, arryRelatedKeyword);
		mLVSearchResult.setAdapter(adapter);

		return v;
	}

}